﻿#include <iostream>

using namespace std;

void printfArray(int *array, int length) {
    for (int i = 0; i < length; ++i) {
        cout << array[i] << " ";
    }
    cout << endl;
}

void selectionSort(int *array, int length) {
    for (int i = 0; i < length - 1; ++i) {
        int index = i;
        int minValue = array[i];

        for (int j = i + 1; j < length; ++j) {
            if (array[j] < minValue) {
                index = j;
                minValue = array[j];
            }
        }

        if (index != i) {
            int temp = array[i];
            array[i] = array[index];
            array[index] = temp;
        }
    }
}

int main() {
    int array[] = {2, 4, 6, 7, 3, 2, 5, 6, 7, 2, 2, 4, 2, 1, 4, 1, 6, 7, 9, 0, 8};
    int length = (sizeof(array) / sizeof(int));
    cout << "length = " << length << endl;

    cout << "排序前: ";
    printfArray(array, length);

    selectionSort(array, length);

    cout << "排序后: ";
    printfArray(array, length);

    return 0;
}